/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package utils;

import java.util.List;
import java.util.Map;

/**
 *
 * @author dingnangen
 */
public class QueryStr {

    private String queryList = "";
    private String queryCount = "";
    private String defaultQueryList = "";
    private Boolean whereflag = false;
    private String table = "";
    private int paramCount = 1;
    private final int type;//0表名,1查询字符串
    private List<String> initpList;

    /**
     *
     * @param s
     * @param type 0-s为表名,1-s为查询语句
     */
    public QueryStr(String s, int type) {
        this.type = type;
        if (type == 0) {
            this.table = s;
            setDefault();
        } else if (type == 1) {
            queryList = s;
            defaultQueryList = s;
        }

    }

    public QueryStr(String s, int type, List<String> pList) {

        initpList = pList;
        this.type = type;
        if (type == 0) {
            this.table = s;
            setDefault();
        } else if (type == 1) {
            queryList = s;
            defaultQueryList = s;
        }
        System.out.println(this.getQueryList());
    }

    /**
     * 设为默认
     */
    public final void setDefault() {
        whereflag = false;
        paramCount = 1;
        if (type == 0) {
            queryList = "select s from " + table + " s ";
            queryCount = "select count(s) from " + table + " s ";
            if (initpList != null) {
                for (String s : initpList) {
                    addEqualCondition("where", s);
                }
            }
        } else if (type == 1) {
            queryList = defaultQueryList;
        }
    }

//    /**
//     *添加where = 查询
//     * @param andor
//     * @param property
//     * @param value
//     */
//    public void addEqualCondition(String andor, String property, String value) { //添加条件
//        if (property != null && !(property.equals("")) && value != null && !(value.equals(""))) {
//
//            queryList += " " + getWhereAndOr(andor) + " s." + property + " = '" + value + "'";
//            queryCount += " " + getWhereAndOr(andor) + " s." + property + " = '" + value + "'";
//        }
//
//    }
    /**
     * 添加where = 查询
     *
     * @param andor
     * @param property
     */
    public void addEqualCondition(String andor, String property) { //添加条件
        if (property != null && !(property.equals(""))) {
            String whereAndOr = getWhereAndOr(andor);
            queryList += " " + whereAndOr + " s." + property + " = ?" + paramCount;
            queryCount += " " + whereAndOr + " s." + property + " = ?" + paramCount;
        }
        paramCount++;
    }

//    /**
//     * 添加like约束条件
//     *
//     * @param andor:传入字符串，where,and,or或其他条件字符串
//     * @param property:参数名
//     * @param value:参数值
//     */
//    public void addLikeCondition(String andor, String property, String value) { //添加条件
//        if (property != null && !(property.equals("")) && value != null && !(value.equals(""))) {
//
//            queryList += " " + getWhereAndOr(andor) + " s." + property + " like '%" + value + "%'";
//            queryCount += " " + getWhereAndOr(andor) + " s." + property + " like '%" + value + "%'";
//        }
//
//    }
    public void addInCondition(String andor, String property, String qstr) {
        if (qstr != null && !(qstr.equals(""))) {
            return;
        }

        String[] split = qstr.split("?");
        qstr = "";
        for (String s : split) {
            qstr = qstr + " ?" + paramCount;
            paramCount++;
        }
        String whereAndOr = getWhereAndOr(andor);
        if (property != null && !(property.equals(""))) {

            queryList += " " + whereAndOr + " s." + property + " in (" + qstr + ")";
            queryCount += " " + whereAndOr + " s." + property + " in (" + qstr + ")";
            System.out.println("queryCount:" + queryCount);
        } else {
            queryList += " " + whereAndOr + " s" + property + " in (" + qstr + ")";
            queryCount += " " + whereAndOr + " s" + property + " in (" + qstr + ")";

        }

    }

    /**
     * 添加like约束条件
     *
     * @param andor:传入字符串，where,and,or或其他条件字符串
     * @param property:参数名
     */
    public void addLikeCondition(String andor, String property) { //添加条件
        if (property != null && !(property.equals(""))) {
            String whereAndOr = getWhereAndOr(andor);
            queryList += " " + whereAndOr + " s." + property + " like ?" + paramCount;
            queryCount += " " + whereAndOr + " s." + property + " like ?" + paramCount;
        }
        paramCount++;
    }

    /**
     * 生成查询结果的排序语句
     *
     * @param sortField
     * @param sortOrder
     */
    public void addorderCondition(String sortField, String sortOrder) {
        if (sortField != null && !(sortField.equals("")) && sortOrder != null && !(sortOrder.equals(""))) {
            queryList += " order by " + "s." + sortField + " " + sortOrder;
            queryCount += " order by " + "s." + sortField + " " + sortOrder;
        }

    }

    /**
     * 根据传入参数返回where,and,or三者之一
     *
     * @param andor：可以是where,and,or
     * @return 返回where,and,or三者之一
     */
    public String getWhereAndOr(String andor) {
        if (andor.equals("where")) {

            System.out.println("1");
            if (whereflag) {
                return " and ";
            } else {
                whereflag = true;
                return " where ";
            }
        } else if (!whereflag) {
            System.out.println("2");
            whereflag = true;
            return " where ";

        } else {
            System.out.println("3");
            return " " + andor + " ";
        }
    }

    /**
     *
     * @return
     */
    public String getQueryList() {
        return queryList;
    }

    /**
     *
     * @param queryList
     */
    public void setQueryList(String queryList) {
        this.queryList = queryList;
    }

    /**
     *
     * @return
     */
    public String getQueryCount() {
        return queryCount;
    }

    /**
     *
     * @param queryCount
     */
    public void setQueryCount(String queryCount) {
        this.queryCount = queryCount;
    }

}
